#!/bin/bash


set -x -e
work_path=$(dirname $(realpath $0))
ramfs=${work_path}/ramfs

initrd_path=${work_path}/initrd
out_rootfs=ramfs.squashfs
out_initrd=initrd.img
out_img_path=${work_path}/out/$(date "+%Y%m%d")
mkdir -p ${out_img_path}
#out_img=${out_img_path}/ramfs-$(date "+%Y%m%d%H%M").ext4
out_img=${work_path}/ramfs.ext4
out_img_size_m=40

sources=http://10.2.10.45/repo/v20/desktop-professional/1054
codec_name=eagle/1054
uninstall_file=${work_path}/config/package/uninstall.list
preinstall_file=${work_path}/config/package/preinstall.list
install_file=${work_path}/config/package/install.list

trap "${work_path}/scripts/umount.sh ${ramfs};exit" SIGHUP SIGINT SIGTERM

uninstall_list=$(cat ${uninstall_file} | tr "\n" " " | sed -e 's/,$/\n/')
preinstall_list=$(cat ${preinstall_file} | tr "\n" "," | sed -e 's/,$/\n/')
install_list=$(cat ${install_file} | tr "\n" " " | sed -e 's/,$/\n/')

if [ -d ${ramfs} ]; then
	rm -rf ${ramfs}
fi

debootstrap --no-check-gpg --include=${preinstall_list}  ${codec_name} ${ramfs}  ${sources} sid

cat > ${ramfs}/etc/resolv.conf << EOF
# Generated by NetworkManager
nameserver 10.2.0.10
EOF

slist=$(cat ${work_path}/config/sources.list)
cat > ${ramfs}/etc/apt/sources.list << EOF
deb [trusted=yes] ${sources} ${codec_name} main contrib non-free
${slist}
EOF

cat > ${ramfs}/tmp/input.txt << EOF
Yes, do as I say!
y
EOF

cp ${work_path}/scripts/{mount.sh,umount.sh} ${ramfs}
pushd ${ramfs}
./mount.sh
popd

chroot ${ramfs} apt purge -y ${uninstall_list}

chroot ${ramfs} bash -c "cat /tmp/input.txt | apt purge systemd ncurses-bin libdebconfclient0 "
chroot ${ramfs} apt purge -y libip4tc0 libjson-c3  libdevmapper1.02.1 libargon2-1 libapparmor1

chroot ${ramfs} ln -sf /bin/bash /bin/sh
chroot ${ramfs} apt update
chroot ${ramfs} apt install -y ${install_list}
chroot ${ramfs} apt autoremove
chroot ${ramfs} apt clean

rm -f ${ramfs}/sbin/init
cp -a ${work_path}/config/rootfs/init ${ramfs}/sbin/
chmod +x ${ramfs}/sbin/init

if [ -x ${work_path}/custom.sh ]; then
	${work_path}/custom.sh ${ramfs}
fi

pushd ${ramfs}
./umount.sh

rm -rf ${ramfs}/usr/share/doc/*
rm -rf ${ramfs}/usr/share/man/*
rm -rf ${ramfs}/usr/share/locale/*
rm -rf ${ramfs}/var/lib/apt/lists/*
rm -rf ${ramfs}/var/cache/*  
rm -rf ${ramfs}/var/log/*  
rm -rf ${ramfs}/tmp/*  
rm  -f ${ramfs}/{mount.sh,umount.sh}
echo "" > ${ramfs}/etc/apt/sources.list
echo "" > ${ramfs}/etc/resolv.conf

if [ -f ${out_img} ]; then
	rm -f ${out_img}
fi

#find . | cpio -o -H newc | gzip -c > ${out_img}
popd

out_tmp=${work_path}/.out

if [ -d ${out_tmp} ]; then
	findmnt --mountpoint ${out_tmp} && umount ${out_tmp} || echo "no mount"
	rm -rf ${out_tmp}
fi
mkdir -p ${out_tmp}

dd if=/dev/zero of=${out_img} bs=1M count=0 seek=${out_img_size_m}
mkfs.ext4 -U 1dd9f24c-9c8e-4fe6-858a-347ccc5cd8bc ${out_img}

mount ${out_img} ${out_tmp}

mksquashfs ${ramfs} ${out_rootfs} -comp zstd
mv ${out_rootfs}  ${out_tmp}/

## gen initrd
cp -a ${work_path}/config/initrd/init ${initrd_path}/
chmod +x ${initrd_path}/init
pushd ${initrd_path}
find . | cpio -o -H newc | gzip -c >  ${out_tmp}/${out_initrd}
popd

ls -sh ${out_tmp}/*
umount ${out_tmp}
rm -rf ${ramfs}
rm -rf ${out_tmp}


